package _06_集合;

import _05_树.tree.BinaryTree;
import _05_树.tree.RBTree;
import _01_第一季._00_公共接口.Set;

import java.util.Comparator;

/*
    内部由红黑树实现的集合
 */
public class TreeSet<E> implements Set<E>{
    private RBTree<E> rbTree;

    public TreeSet(){
        this(null);
    }

    public TreeSet(Comparator<E> comparator){
        rbTree = new RBTree<>();
    }

    @Override
    public int size() {
        return rbTree.size();
    }

    @Override
    public boolean isEmpty() {
        return rbTree.isEmpty();
    }

    @Override
    public void clear() {
        rbTree.clear();
    }

    @Override
    public boolean contains(E element) {
        return rbTree.contains(element);
    }

    @Override
    public void add(E element) {
        rbTree.add(element);
    }

    @Override
    public void remove(E element) {
        rbTree.remove(element);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        rbTree.inOrder(new BinaryTree.Visitor<E>() {
            @Override
            public boolean visit(E element) {
                return visitor.visit(element);
            }
        });
    }
}
